package com.itaming.lycheeframework.mybatisflex.support;

import com.itaming.lycheeframework.support.utils.CollectionUtil;
import com.itaming.lycheeframework.support.utils.StringPool;
import com.itaming.lycheeframework.support.utils.StringUtil;
import com.itaming.lycheeframework.support.web.Query;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.SqlOperators;
import lombok.experimental.UtilityClass;

import java.util.List;
import java.util.Map;

/**
 * 查询助手类
 *
 * @author A.Ming
 */
@UtilityClass
public class QueryHelper {

    /**
     * 获取分页对象
     *
     * @param query 分页排序查询参数
     * @return 分页对象
     */
    public static <T> Page<T> page(Query query) {
        Page<T> page = new Page<>();

        if (query != null) {
            // 当前页
            if (query.getPage() != null && query.getPage() >= 1) {
                page.setPageNumber(query.getPage());
            }

            // 每页条数
            if (query.getSize() != null && query.getSize() >= 1) {
                page.setPageSize(query.getSize());
            }
        }

        return page;
    }

    /**
     * 获取QueryWrapper
     *
     * @param entity 查询条件实体对象
     * @return QueryWrapper
     */
    public static QueryWrapper wrapper(Object entity) {
        return wrapper(entity, null, null);
    }

    /**
     * 获取QueryWrapper
     *
     * @param entity 查询条件实体对象
     * @param query  分页排序查询参数
     * @return QueryWrapper
     */
    public static QueryWrapper wrapper(Object entity, Query query) {
        return wrapper(entity, query, null);
    }

    /**
     * 获取QueryWrapper
     *
     * @param entity    查询条件实体对象
     * @param operators 字段查询类型
     * @return QueryWrapper
     */
    public static QueryWrapper wrapper(Object entity, SqlOperators operators) {
        return wrapper(entity, null, operators);
    }

    /**
     * 获取QueryWrapper
     *
     * @param entity    查询条件实体对象
     * @param query     分页排序查询参数
     * @param operators 字段查询类型
     * @return QueryWrapper
     */
    public static QueryWrapper wrapper(Object entity, Query query, SqlOperators operators) {
        return handleQueryWrapper(QueryWrapper.create(entity, operators), query);
    }

    /**
     * 获取QueryWrapper
     *
     * @param map 查询条件
     * @return QueryWrapper
     */
    public static QueryWrapper wrapper(Map<String, Object> map) {
        return wrapper(map, null, null);
    }

    /**
     * 获取QueryWrapper
     *
     * @param map   查询条件
     * @param query 分页排序查询参数
     * @return QueryWrapper
     */
    public static QueryWrapper wrapper(Map<String, Object> map, Query query) {
        return wrapper(map, query, null);
    }

    /**
     * 获取QueryWrapper
     *
     * @param map       查询条件
     * @param operators 字段查询类型
     * @return QueryWrapper
     */
    public static QueryWrapper wrapper(Map<String, Object> map, SqlOperators operators) {
        return wrapper(map, null, operators);
    }

    /**
     * 获取QueryWrapper
     *
     * @param map       查询条件
     * @param query     分页排序查询参数
     * @param operators 字段查询类型
     * @return QueryWrapper
     */
    public static QueryWrapper wrapper(Map<String, Object> map, Query query, SqlOperators operators) {
        return handleQueryWrapper(QueryWrapper.create(map, operators), query);
    }

    /**
     * 处理QueryWrapper
     *
     * @param queryWrapper QueryWrapper
     * @param query        Query
     * @return QueryWrapper
     */
    private static QueryWrapper handleQueryWrapper(QueryWrapper queryWrapper, Query query) {
        if (query != null) {
            addOrderBy(queryWrapper, query.getSort());
        }

        return queryWrapper;
    }

    /**
     * 添加排序字段
     *
     * @param queryWrapper QueryWrapper
     * @param sort         排序字段列表
     */
    private static void addOrderBy(QueryWrapper queryWrapper, List<String> sort) {
        if (CollectionUtil.isEmpty(sort)) {
            return;
        }

        for (String s : sort) {
            if (StringUtil.isBlank(s)) {
                continue;
            }

            if (!StringUtil.contains(s, StringPool.COLON)) {
                queryWrapper.orderBy(s, true);
                continue;
            }

            String[] split = StringUtil.split(s, StringPool.COLON);

            switch (StringUtil.strip(split[1])) {
                case "":
                case "asc":
                    queryWrapper.orderBy(StringUtil.strip(split[0]), true);
                    break;
                case "desc":
                    queryWrapper.orderBy(StringUtil.strip(split[0]), false);
                    break;
                default:
                    break;
            }
        }
    }

}
